<?php
class GanJiFunc
{

    static $tianGan=array(0=>'甲',1=>'乙',2=>'丙',3=>'丁',4=>'戊',5=>'己',6=>'庚',7=>'辛',8=>'壬',9=>'癸');
    static $tianGan2Id=array('甲'=>0,'乙'=>1,'丙'=>2,'丁'=>3,'戊'=>4,'己'=>5,'庚'=>6,'辛'=>7,'壬'=>8,'癸'=>9);
    static $diJi=array(0=>'子',1=>'丑',2=>'寅',3=>'卯',4=>'辰',5=>'巳',6=>'午',7=>'未',8=>'申',9=>'酉',10=>'戌',11=>'亥');
    static $diJi2Id=array('子'=>0,'丑'=>1,'寅'=>2,'卯'=>3,'辰'=>4,'巳'=>5,'午'=>6,'未'=>7,'申'=>8,'酉'=>9,'戌'=>10,'亥'=>11);

    static function prevGan($gan)
    {
        $id=self::$tianGan2Id[$gan];
        $id=($id+10-1)%10;
        return self::$tianGan[$id];        
    }
    static function prevJi($ji)
    {
        $id=self::$diJi2Id[$ji];
        $id=($id+12-1)%12;
        return self::$diJi[$id];
    }
    static  function nextGan($gan)
    {
        $id=self::$tianGan2Id[$gan];
        $id=($id+1)%10;
        return self::$tianGan[$id];
        
    }
    static  function nextJi($Ji)
    {
        $id=self::$diJi2Id[$Ji];
        $id=($id+1)%12;
        return self::$diJi[$id];
    }
    
    static function isYanGan($gan)
    {
        if(self::$tianGan2Id[$gan]%2==0)
            return true;
        return false;            
    }
    static function isYanJi($ji)
    {
        if(self::$diJi2Id[$ji]%2==0)
            return true;
        return false;
    }
    function get6WordByScalar($y,$m,$d)
    {
        $fn='../YellowCal/convert/month/'.$y.'-'.$m.'.xml';
        if(file_exists($fn)==false)
            return null;
        $ds=$y.'/'.$m.'/'.$d;
        $xml = simplexml_load_file($fn);
    
        foreach($xml->day as $c){
            if($c->date==$ds)
            {
                $result=array();
                $result['date']=$c->date;
                $result['年干支']=self::splitGJ((string)$c->GJ_year);
                $result['月干支']=self::splitGJ((string)$c->GJ_month);
                $result['日干支']=self::splitGJ((string)$c->GJ_day);
    
                return $result;
            }
        }
    
    }
    
    static function splitGJ($s)
    {
        $result=array();
        $s=trim($s);
        $result['干支']=$s;
        $result['干']=mb_substr($s,0,1,'UTF-8');
        $result['支']=mb_substr($s,1,1,'UTF-8');
        return $result;
    }


    static function getHrGanJi($dayGan,$hour,$minute)
    {
        $hour=$hour%24;
        $mins=$hour*60+$minute;

        $mins+=60;
        $hrid=(int)($mins/120);
        $HrJi=self::$diJi[$hrid];


        $ganidx=self::$tianGan2Id[$dayGan];
        $hrg=($ganidx*12+$hrid)%10;
        $s=self::$tianGan[$hrg].$HrJi;
        return self::splitGJ($s);
    }

    static function isValidTime($y,$m,$d,$h=0,$i=0)
    {
        return true;
    }
    static  function generateEightWord($y,$m,$d,$h=0,$i=0)
    {
        if(self::isValidTime($y, $m, $d,$h,$i))
        {
            $result=self::get6WordByScalar($y, $m, $d);
            $result['時干支']=self::getHrGanJi($result['日干支']['干'], $h, $i);
            var_dump($result);
            return $result;
        }
        
    }


}